﻿using Abp.AspNetCore.Mvc.Controllers;
using Abp.Auditing;
using Abp.Dependency;
using Abp.Extensions;
using Abp.Web.Models;
using Microsoft.AspNetCore.Mvc;
using Mt.Site.Application.Net.MimeTypes;
using Mt.Site.Core.Base.BlobStoring;
using Mt.Site.Core.Base.Utils.FileIO;
using Mt.Site.Core.Base.Utils.FileIO.Down;
using Mt.Site.Core.Base.Utils.ImageProcess;
using Mt.Site.Core.VerifyCodes;
using Polly;
using System;
using System.IO;
using System.Threading.Tasks;

namespace Mt.Site.Web.Controllers
{
    public  class UtilsController : Controller, ITransientDependency
    {
        [DisableAuditing]
        [IgnoreAntiforgeryToken]
        [HttpGet]
        [DontWrapResult]
        public async Task<FileResult> verifyCode()
        {
            using (var verifyCodeManage = IocManager.Instance.ResolveAsDisposable<IVerifyCodeManager>())
            {
                var bytes = await verifyCodeManage.Object.GetVerifyCodeAndResponse();

               return File(bytes, MimeTypeNames.ImagePng);
            }
        }

        [DisableAuditing]
        [IgnoreAntiforgeryToken]
        [HttpGet]
        [DontWrapResult]
        //文件下载
        public async Task Down(string paraName ="myfile", string fileName = null)
        {
            using (var fileDown = IocManager.Instance.ResolveAsDisposable<IFileDown>())
            {
                fileDown.Object.Initialize(null, null, paraName, fileName);
                await fileDown.Object.Done();
            }
        }

        ////生成缩略图
        //[DisableAuditing]
        //[IgnoreAntiforgeryToken]
        //[HttpGet]
        //[DontWrapResult]
        //public async Task<ActionResult> Zoomin(string path, int w,int h, ScaleMode mode)
        //{
        //    var exName = Path.GetExtension(path);
        //    var newName = $"{path.ToMd5()}-{w}-{h}-{mode}{exName}";

        //    using (var scope = IocManager.Instance.CreateScope())
        //    {
        //        // 查找缓存文件
        //        var appFolders = scope.Resolve<IAppFolders>();
        //        var cachePath = Path.Combine(appFolders.TempFileFolder, newName);
        //        if (!System.IO.File.Exists(cachePath))
        //        {
        //            var blobContainer = scope.Resolve<IBlobContainer>();
        //            using (var stream = new MemoryStream())
        //            {
        //                var dataStream = await blobContainer.GetOrNullAsync(path);
        //                if(dataStream != null)
        //                {
        //                    await dataStream.CopyToAsync(stream);
        //                    // 生成缓存图
        //                    var imageHelper = scope.Resolve<IImageHelper>();
        //                    try
        //                    {
        //                        ImageResult imageResult = imageHelper.AutoAdjust(
        //                            new ImageZoomContext(
        //                                stream,
        //                                w, h, mode, 75, true)
        //                            );
        //                        using(imageResult.Stream)
        //                        {
        //                            await Policy.Handle<IOException>()
        //                                .WaitAndRetryAsync(2, retryCount => TimeSpan.FromSeconds(retryCount))
        //                                .ExecuteAsync(async () =>
        //                                {
        //                                    using (var fileStream = System.IO.File.Open(cachePath, FileMode.Create, FileAccess.Write))
        //                                    {
        //                                        await imageResult.Stream.CopyToAsync(
        //                                            fileStream);

        //                                        await fileStream.FlushAsync();
        //                                    }
        //                                });
        //                        }
        //                    }
        //                    catch //(Exception ex)
        //                    {
        //                        // Logger.Error(ex.ToString());
        //                        // throw new Exception(ex.ToString());
        //                        // return File("/content/defaultImg/DefaultImage.jpg", "image/png");
        //                        return File(stream.GetBuffer(), "image/png");
        //                    }
        //                }
        //                else
        //                {
        //                    return File("/content/defaultImg/DefaultImage.jpg", "image/png");
        //                }
        //            }
        //        }

        //        return File(FileHelper.GetRelativePath(cachePath), "image/png");
        //    }
        //}
    }
}
